<template>
    <div>
        <div class="add">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="课题编号">
                    <el-input v-model="form.subjectId"></el-input>
                </el-form-item>
                <el-form-item label="课题名称">
                    <el-input v-model="form.subjectName"></el-input>
                </el-form-item>
                <el-form-item label="预选人数">
                    <el-input v-model="form.primaryNum"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addSubject('form')">添加</el-button>
                    <el-button @click="$router.back(-1)">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import  api from '@/api/subject/subjectApi'
  export default {
    data() {
      return {
        form: {
          subjectId: '',
          subjectName: '',
          primaryNum: ''
        }
      }
    },
 
    methods: {
    
      addSubject(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              api.addSubject(this.form)
              .then(response=>{
                this.$alert('添加成功', '成功', {
                  dangerouslyUseHTMLString: true
                });
                this.clean();
                window.location.reload()
              }).catch(error =>{
                this.$alert('<strong>添加失败</strong>', '失败', {
                  dangerouslyUseHTMLString: true
                });
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      clean(){
          this.subjectId="",
          this.subjectName="",
          this. primaryNum=""

      }
      
    }
  }
</script>
<style>
.add{
  width: 500px;
  margin: 100px auto;

}
</style>